<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="keyword"  content="">
    <link rel="shortcut icon" href="/hexo-blog/img/ironman-draw.png">
    <!-- Place this tag in your head or just before your close body tag. -->
    <script async defer src="https://buttons.github.io/buttons.js"></script>
    <title>
        
          如何使用css3特性scroll-snap-type替代sticky布局 - 管涛 | Blog
        
    </title>

    <link rel="canonical" href="https://guantaocc.github.io/hexo-blog/hexo-blog/css3/scroll-snap-type/">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/hexo-blog/css/bootstrap.min.css">

    <!-- Custom CSS --> 
    <link rel="stylesheet" href="/hexo-blog/css/beantech.min.css">

    <link rel="stylesheet" href="/hexo-blog/css/donate.css">
    
    <!-- Pygments Highlight CSS -->
    <link rel="stylesheet" href="/hexo-blog/css/highlight.css">

    <link rel="stylesheet" href="/hexo-blog/css/widget.css">

    <link rel="stylesheet" href="/hexo-blog/css/rocket.css">

    <link rel="stylesheet" href="/hexo-blog/css/signature.css">

    <link rel="stylesheet" href="/hexo-blog/css/toc.css">

    <!-- Custom Fonts -->
    <!-- <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> -->
    <!-- Hux change font-awesome CDN to qiniu -->
    <link href="https://cdn.staticfile.org/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">


    <!-- Hux Delete, sad but pending in China
    <link href='http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/
    css'>
    -->


    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- ga & ba script hoook -->
    <script></script>
</head>


<!-- hack iOS CSS :active style -->
<body ontouchstart="">
	<!-- Modified by Yu-Hsuan Yen -->
<!-- Post Header -->
<style type="text/css">
    header.intro-header{
        
            background-image: url('/img/article_header/article_header.png')
            /*post*/
        
    }
    
</style>

<header class="intro-header" >
    <!-- Signature -->
    <div id="signature">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                
                    <div class="post-heading">
                        <div class="tags">
                            
                              <a class="tag" href="/hexo-blog/tags/#css3" title="css3">css3</a>
                            
                        </div>
                        <h1>如何使用css3特性scroll-snap-type替代sticky布局</h1>
                        <h2 class="subheading"></h2>
                        <span class="meta">
                            Posted by 管涛 on
                            2019-09-06
                        </span>
                    </div>
                


                </div>
            </div>
        </div>
    </div>
</header>

	
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/hexo-blog/">管涛</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/hexo-blog/">Home</a>
                    </li>

                    

                        
                    

                        
                        <li>
                            <a href="/hexo-blog/archive/">Archives</a>
                        </li>
                        
                    

                        
                        <li>
                            <a href="/hexo-blog/tags/">Tags</a>
                        </li>
                        
                    

                        
                        <li>
                            <a href="/hexo-blog/about/">关于</a>
                        </li>
                        
                    
                    
                </ul>
            </div>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>


    <!-- Main Content -->
    <!-- Modify by Yu-Hsuan Yen -->

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Post Container -->
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                post-container">

                <blockquote>
<p>css3新特性效果MDN解释 <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type" target="_blank" rel="noopener">https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type</a><br>
如果要使就浏览器支持: 你应该使用这个插件<a href="https://www.npmjs.com/package/css-scroll-snap-polyfill" target="_blank" rel="noopener">https://www.npmjs.com/package/css-scroll-snap-polyfill</a></p>
</blockquote>
<h2 id="传统sticky介绍">传统sticky介绍</h2>
<p>传统的position:sticky效果实现，基于堆叠的代码效果:<br>
html:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;div class=&quot;vertical-scroll-snap&quot;&gt;</span><br><span class="line">   &lt;section class=&quot;stacking-slide&quot;&gt;</span><br><span class="line">     &lt;h2&gt;Section 1&lt;/h2&gt;</span><br><span class="line">     &lt;p&gt;use scroll snap type : Lorem ipsum, dolor sit amet consectetur adipisicing elit. Molestiae aspernatur odit vero</span><br><span class="line">       quaerat. Enim maxime laudantium minima, voluptatum fugiat error,</span><br><span class="line">       dolorem reiciendis cupiditate odit doloremque illum porro ducimus, accusamus omnis.&lt;/p&gt;</span><br><span class="line">   &lt;/section&gt;</span><br><span class="line">   &lt;section class=&quot;stacking-slide&quot;&gt;</span><br><span class="line">     &lt;h2&gt;Section 2&lt;/h2&gt;</span><br><span class="line">     /* ... */</span><br><span class="line">   &lt;/section&gt;</span><br><span class="line">   &lt;section class=&quot;stacking-slide&quot;&gt;</span><br><span class="line">     &lt;h2&gt;Section 3&lt;/h2&gt;</span><br><span class="line">   &lt;/section&gt;</span><br><span class="line">   &lt;section class=&quot;stacking-slide&quot;&gt;</span><br><span class="line">     &lt;h2&gt;Section 4&lt;/h2&gt;</span><br><span class="line">   &lt;/section&gt;</span><br><span class="line">   &lt;section class=&quot;stacking-slide&quot;&gt;</span><br><span class="line">     &lt;h2&gt;Section 5&lt;/h2&gt;</span><br><span class="line">   &lt;/section&gt;</span><br><span class="line">   &lt;!-- ... --&gt;</span><br><span class="line"> &lt;/div&gt;</span><br></pre></td></tr></table></figure>
<p>css:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">html,</span><br><span class="line">    body,</span><br><span class="line">    h2 &#123;</span><br><span class="line">      margin: 0;</span><br><span class="line">      padding: 0;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    .stacking-slide &#123;</span><br><span class="line">      height: 100vh;</span><br><span class="line">      width: 100%;</span><br><span class="line">      position: sticky;</span><br><span class="line">      position: -webkit-sticky;</span><br><span class="line">      top: 0px;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    .stacking-slide:nth-child(odd) &#123;</span><br><span class="line">      background-color: burlywood;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    .stacking-slide:nth-child(even) &#123;</span><br><span class="line">      background-color: darkseagreen;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="基于css3的效果实现">基于css3的效果实现</h2>
<p>css3中提供了更为平滑的使用粘性布局的方法:<br>
scroll-snap-type: x mandatory;<br>
scroll-snap-type: y proximity;<br>
scroll-snap-type: both mandatory;</p>
<p>其中第一个参数指定位置，第二个参数指定平滑位置。</p>
<p>使用方式:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">.vertical-scroll-snap &#123;</span><br><span class="line">      overflow-y: scroll;</span><br><span class="line">      scroll-snap-type: y mandatory;</span><br><span class="line">      max-height: 100vh;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    .stacking-slide &#123;</span><br><span class="line">      scroll-snap-align: start;</span><br><span class="line">      height: 100vh;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="更高级的滑动效果实现使用intersection-observe-api">更高级的滑动效果实现(使用intersection Observe API)</h2>
<blockquote>
<p>MDN介绍<a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API" target="_blank" rel="noopener">https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API</a><br>
下面的例子实现了当slide进入视口10%时加入效果，当他离开时我们移除效果：<br>
observe API: root(检测节点) rootMargin(属性), threshold(视口元素触发百分比)</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">let options = &#123;</span><br><span class="line">  root: document.querySelector(&apos;#scrollArea&apos;),</span><br><span class="line">  rootMargin: &apos;0px&apos;,</span><br><span class="line">  threshold: 1.0</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">let observer = new IntersectionObserver(callback, options);</span><br></pre></td></tr></table></figure>
<h3 id="完整代码">完整代码:</h3>
<p>html</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">&lt;div class=&quot;vertical-scroll-snap&quot;&gt;</span><br><span class="line">    &lt;section class=&quot;stacking-slide&quot;&gt;</span><br><span class="line">      &lt;h2&gt;Section 1&lt;/h2&gt;</span><br><span class="line">      &lt;p&gt;</span><br><span class="line">        Lorem ipsum, dolor sit amet consectetur adipisicing elit. Molestiae aspernatur odit vero quaerat. Enim maxime</span><br><span class="line">        laudantium minima, voluptatum fugiat error, dolorem reiciendis cupiditate odit doloremque illum porro ducimus,</span><br><span class="line">        accusamus omnis.</span><br><span class="line">      &lt;/p&gt;</span><br><span class="line">    &lt;/section&gt;</span><br><span class="line">    &lt;section class=&quot;stacking-slide&quot;&gt;</span><br><span class="line">      &lt;h2&gt;Section 2&lt;/h2&gt;</span><br><span class="line">      &lt;p&gt;</span><br><span class="line">        Lorem ipsum dolor, sit amet consectetur adipisicing elit. Mollitia tempora aperiam rem perferendis alias animi</span><br><span class="line">        dolorum reiciendis vitae nobis explicabo similique doloremque, inventore possimus aliquid harum maiores eveniet</span><br><span class="line">        expedita quibusdam.</span><br><span class="line">      &lt;/p&gt;</span><br><span class="line">      &lt;div class=&quot;boxes&quot;&gt;</span><br><span class="line">        &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;</span><br><span class="line">        &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;</span><br><span class="line">        &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    &lt;/section&gt;</span><br><span class="line">    &lt;section class=&quot;stacking-slide&quot;&gt;</span><br><span class="line">      &lt;h2&gt;Section 3&lt;/h2&gt;</span><br><span class="line">      &lt;p&gt;</span><br><span class="line">        Lorem ipsum dolor, sit amet consectetur adipisicing elit. Commodi mollitia earum quia porro dignissimos</span><br><span class="line">        laboriosam pariatur excepturi molestiae odit temporibus repellendus, dolores quae ipsum quidem. Nam</span><br><span class="line">        exercitationem voluptate in a.</span><br><span class="line">      &lt;/p&gt;</span><br><span class="line">      &lt;div class=&quot;boxes&quot;&gt;</span><br><span class="line">        &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    &lt;/section&gt;</span><br><span class="line">    &lt;section class=&quot;stacking-slide&quot;&gt;</span><br><span class="line">      &lt;h2&gt;Section 4&lt;/h2&gt;</span><br><span class="line">      &lt;p&gt;Lorem ipsum dolor sit amet consectetur, adipisicing elit. Id expedita vero fugit unde saepe maxime sint</span><br><span class="line">        inventore similique ad blanditiis veniam eos, possimus molestias, assumenda ipsa! Excepturi illo accusantium at!</span><br><span class="line">      &lt;/p&gt;</span><br><span class="line">      &lt;div class=&quot;boxes&quot;&gt;</span><br><span class="line">        &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;</span><br><span class="line">        &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;</span><br><span class="line">        &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    &lt;/section&gt;</span><br><span class="line">    &lt;section class=&quot;stacking-slide&quot;&gt;</span><br><span class="line">      &lt;h2&gt;Section 5&lt;/h2&gt;</span><br><span class="line">      &lt;p&gt;</span><br><span class="line">        Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquid, sit suscipit voluptatem nisi nemo aliquam</span><br><span class="line">        quisquam provident sequi delectus veniam ducimus? Expedita repudiandae sint molestias. Distinctio quo itaque</span><br><span class="line">        numquam voluptatum.</span><br><span class="line">      &lt;/p&gt;</span><br><span class="line">      &lt;div class=&quot;boxes&quot;&gt;</span><br><span class="line">        &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;</span><br><span class="line">        &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    &lt;/section&gt;</span><br><span class="line">  &lt;/div&gt;</span><br></pre></td></tr></table></figure>
<p>CSS:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line">html,</span><br><span class="line">   body,</span><br><span class="line">   h2 &#123;</span><br><span class="line">     margin: 0;</span><br><span class="line">     padding: 0;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   /* .stacking-slide &#123;</span><br><span class="line">     height: 100vh;</span><br><span class="line">     width: 100%;</span><br><span class="line">     position: sticky;</span><br><span class="line">     position: -webkit-sticky;</span><br><span class="line">     top: 0px;</span><br><span class="line">   &#125; */</span><br><span class="line">   /* 运用scroll-snap-type特性 */</span><br><span class="line">   .vertical-scroll-snap &#123;</span><br><span class="line">     overflow-y: scroll;</span><br><span class="line">     scroll-snap-type: y mandatory;</span><br><span class="line">     max-height: 100vh;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   .stacking-slide &#123;</span><br><span class="line">     scroll-snap-align: start;</span><br><span class="line">     height: 100vh;</span><br><span class="line">     transition: all 0.1.5s ease-in-out;</span><br><span class="line"></span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   .stacking-slide * &#123;</span><br><span class="line">     opacity: 0.2;</span><br><span class="line">     transform: translateY(8rem) scale(0.7);</span><br><span class="line">     transition: all 1.5s ease-in-out;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   .stacking-slide.is-intersecting * &#123;</span><br><span class="line">     opacity: 1;</span><br><span class="line">     transform: translatey(0) scale(1);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   .boxes &#123;</span><br><span class="line">     display: flex;</span><br><span class="line">     flex-wrap: wrap;</span><br><span class="line">     margin-bottom: 1rem;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   .boxes .box &#123;</span><br><span class="line">     box-shadow: 0 0px 50px -12px rgba(0, 0, 0, 0.05);</span><br><span class="line">     background-color: hsl(190, 100%, 68%);</span><br><span class="line">     padding-bottom: 200px;</span><br><span class="line">     width: 200px;</span><br><span class="line">     margin: 1rem;</span><br><span class="line"></span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   .box:nth-child(1) &#123;</span><br><span class="line">     transform: translate(8rem, 2rem) scale(1.2);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   .box:nth-child(2) &#123;</span><br><span class="line">     transform: translate(-2rem, -5rem) scale(0.5);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   .box:nth-child(3) &#123;</span><br><span class="line">     transform: translate(3rem, 5rem) scale(0.2);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   .stacking-slide:nth-child(odd) &#123;</span><br><span class="line">     background-color: burlywood;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   .stacking-slide:nth-child(even) &#123;</span><br><span class="line">     background-color: darkseagreen;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<p>JS：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">const sectionEls = document.querySelectorAll(&quot;.stacking-slide&quot;);</span><br><span class="line"> const options = &#123;</span><br><span class="line">   rootMargin: &quot;-10% 0% -10% 0%&quot;</span><br><span class="line"> &#125;;</span><br><span class="line"> const observer = new IntersectionObserver(function (entries) &#123;</span><br><span class="line">   entries.forEach(function (entry) &#123;</span><br><span class="line">     if (entry.isIntersecting) &#123;</span><br><span class="line">       entry.target.classList.add(&quot;is-intersecting&quot;);</span><br><span class="line">     &#125; else &#123;</span><br><span class="line">       entry.target.classList.remove(&quot;is-intersecting&quot;);</span><br><span class="line">     &#125;</span><br><span class="line">   &#125;)</span><br><span class="line"> &#125;, options)</span><br><span class="line"> // 为每个元素添加监听</span><br><span class="line"> sectionEls.forEach(function (el) &#123;</span><br><span class="line">   observer.observe(el);</span><br><span class="line"> &#125;);</span><br></pre></td></tr></table></figure>

                

                <hr>
                <!-- Pager -->
                <ul class="pager">
                    
                    
                        <li class="next">
                            <a href="/hexo-blog/hexo/从零使用hexo部署自己的个人博客/" data-toggle="tooltip" data-placement="top" title="'从零使用hexo部署自己的个人博客'">Next Post &rarr;</a>
                        </li>
                    
                </ul>

                <br>

                <!--打赏-->
                
                <!--打赏-->

                <br>
                <!--分享-->
                
                    <div class="social-share"  data-wechat-qrcode-helper="" align="center"></div>
                    <!--  css & js -->
                    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css">
                    <script src="https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script>
                
                <!--分享-->
                <br>                       
                
                <!-- require APlayer -->
                

                <!-- duoshuo Share start -->
                
                <!-- 多说 Share end-->

                <!-- 多说评论框 start -->
                
                <!-- 多说评论框 end -->

                <!-- disqus comment start -->
                
                <!-- disqus comment end -->

                

            </div>
            
            <!-- Tabe of Content -->
            <!-- Table of Contents -->

  
    
      <aside id="sidebar">
        <div id="toc" class="toc-article">
        <strong class="toc-title">Contents</strong>
        
          <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#传统sticky介绍"><span class="toc-nav-number">1.</span> <span class="toc-nav-text">&#x4F20;&#x7EDF;sticky&#x4ECB;&#x7ECD;</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#基于css3的效果实现"><span class="toc-nav-number">2.</span> <span class="toc-nav-text">&#x57FA;&#x4E8E;css3&#x7684;&#x6548;&#x679C;&#x5B9E;&#x73B0;</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#更高级的滑动效果实现使用intersection-observe-api"><span class="toc-nav-number">3.</span> <span class="toc-nav-text">&#x66F4;&#x9AD8;&#x7EA7;&#x7684;&#x6ED1;&#x52A8;&#x6548;&#x679C;&#x5B9E;&#x73B0;(&#x4F7F;&#x7528;intersection Observe API)</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#完整代码"><span class="toc-nav-number">3.1.</span> <span class="toc-nav-text">&#x5B8C;&#x6574;&#x4EE3;&#x7801;:</span></a></li></ol></li></ol>
        
        </div>
      </aside>
    

                
            <!-- Sidebar Container -->
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                
                <section>
                    <!-- no hr -->
                    <h5><a href="/hexo-blog/tags/">FEATURED TAGS</a></h5>
                    <div class="tags">
                       
                          <a class="tag" href="/hexo-blog/tags/#css3" title="css3">css3</a>
                        
                    </div>
                </section>
                

                <!-- Friends Blog -->
                
            </div>
        </div>
    </div>
</article>








<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>
<!-- anchor-js, Doc:http://bryanbraun.github.io/anchorjs/ -->
<script>
    async("https://cdn.bootcss.com/anchor-js/1.1.1/anchor.min.js",function(){
        anchors.options = {
          visible: 'hover',
          placement: 'left',
          icon: 'ℬ'
        };
        anchors.add().remove('.intro-header h1').remove('.subheading').remove('.sidebar-container h5');
    })
</script>
<style>
    /* place left on bigger screen */
    @media all and (min-width: 800px) {
        .anchorjs-link{
            position: absolute;
            left: -0.75em;
            font-size: 1.1em;
            margin-top : -0.1em;
        }
    }
</style>


<!-- chrome Firefox 中文锚点定位失效-->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<!-- smooth scroll behavior polyfill  -->
<script type="text/javascript" src="/js/smoothscroll.js"></script>
<script>
        $('#toc').on('click','a',function(a){
            // var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1;
            // console.log(window.navigator.userAgent,isChrome)
                // if(isChrome) {
                    // console.log(a.currentTarget.outerHTML);
                    // console.log($(a.currentTarget).attr("href"));
                    //跳转到指定锚点
                    // document.getElementById(a.target.innerText.toLowerCase()).scrollIntoView(true);
                    document.getElementById($(a.currentTarget).attr("href").replace("#","")).scrollIntoView({behavior: 'smooth' });
                // }
        })  
</script>


    <!-- Footer -->
    <!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                
                
                

                

                

                
                    <li>
                        <a target="_blank"  href="https://github.com/guantaocc">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                

                </ul>
                <p class="copyright text-muted">
                    Copyright &copy; 管涛 2019 
                    <br>
                    Theme by <a href="http://beantech.org">BeanTech</a> 
                    <span style="display: inline-block; margin: 0 5px;">
                        <i class="fa fa-heart"></i>
                    </span> 
                    re-Ported by <a href="http://www.huweihuang.com">胡伟煌</a> | 
                    <iframe
                        style="margin-left: 2px; margin-bottom:-5px;"
                        frameborder="0" scrolling="0" width="91px" height="20px"
                        src="https://ghbtns.com/github-btn.html?user=huweihuang&repo=hexo-theme-huweihuang&type=star&count=true" >
                    </iframe>
                </p>
            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->
<script src="/hexo-blog/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/hexo-blog/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/hexo-blog/js/hux-blog.min.js"></script>


<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- 
     Because of the native support for backtick-style fenced code blocks 
     right within the Markdown is landed in Github Pages, 
     From V1.6, There is no need for Highlight.js, 
     so Huxblog drops it officially.

     - https://github.com/blog/2100-github-pages-now-faster-and-simpler-with-jekyll-3-0  
     - https://help.github.com/articles/creating-and-highlighting-code-blocks/    
-->
<!--
    <script>
        async("http://cdn.bootcss.com/highlight.js/8.6/highlight.min.js", function(){
            hljs.initHighlightingOnLoad();
        })
    </script>
    <link href="http://cdn.bootcss.com/highlight.js/8.6/styles/github.min.css" rel="stylesheet">
-->


<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("https://guantaocc.github.io/hexo-blog/hexo-blog/js/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>


<!-- Google Analytics -->


<script>
    // dynamic User by Hux
    var _gaId = 'UA-XXXXXXXX-X';
    var _gaDomain = 'yoursite';

    // Originial
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', _gaId, _gaDomain);
    ga('send', 'pageview');
</script>




<!-- Baidu Tongji -->

<script>
    // dynamic User by Hux
    var _baId = 'xxx';

    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>






	<a id="rocket" href="#top" class=""></a>
	<script type="text/javascript" src="/js/totop.js?v=1.0.0" async=""></script>
    <script type="text/javascript" src="/js/toc.js?v=1.0.0" async=""></script>
<!-- Image to hack wechat -->
<img src="https://guantaocc.github.io/hexo-blog/hexo-blog/img/icon_wechat.png" width="0" height="0" />
<!-- Migrate from head to bottom, no longer block render and still work -->

</body>

</html>
